/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UsuarioTablePane.java
 *
 * Created on Jul 6, 2011, 11:09:36 PM
 */
package framework.parts;

import framework.config.Utilities;
import framework.data.Quarto;
import framework.db.QuartoManager;
import framework.editor.QuartoEditor;
import java.awt.Frame;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Darlan
 */
public class QuartoTablePane extends javax.swing.JPanel
{

    /** Creates new form UsuarioTablePane */
    public QuartoTablePane()
    {
        initComponents();
        
        refreshContent();
    }
    
    /**
     * 
     */
    public void refreshContent()
    {
        try
        {
            List<Quarto> pessoas = QuartoManager.getInstance().getQuarto();
            
            quartoTable1.setPessoas( pessoas );
        }
                
        catch ( Exception ex )
        {
            Utilities.handleException( ex );
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        quartoTable1 = new framework.parts.QuartoTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Quartos"));
        setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jButton1.setText("Adicionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(jButton1, gridBagConstraints);

        jButton2.setText("Editar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(jButton2, gridBagConstraints);

        jButton3.setText("Deletar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jButton3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        add(jPanel1, gridBagConstraints);

        quartoTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(quartoTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jScrollPane1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
        Quarto p = quartoTable1.getSelectedQuarto();
        
        if ( p != null )
        {
            try
            {
                if ( QuartoEditor.edit( (Frame) getTopLevelAncestor(), p ) )
                {
                    QuartoManager.getInstance().updateQuarto( p );
                    
                    refreshContent();
                }
            }

            catch ( Exception e )
            {
                Utilities.handleException( e );
            }
        }
        
        else
        {
            JOptionPane.showMessageDialog( this, "É necessário selecionar uma pessoa na lista" );
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        Quarto p = new Quarto();
        
        try
        {
            if ( QuartoEditor.edit( (Frame) getTopLevelAncestor(), p ) )
            {
                QuartoManager.getInstance().addQuarto( p );
                
                refreshContent();
            }
        }
        
        catch ( Exception e )
        {
            Utilities.handleException( e );
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
    {//GEN-HEADEREND:event_jButton3ActionPerformed
        Quarto p = quartoTable1.getSelectedQuarto();
        
        if ( p != null )
        {
            try
            {
                if ( JOptionPane.showConfirmDialog( this, "Tem certeza que deseja excuir a pessoa selecionada?" ) 
                        == JOptionPane.OK_OPTION )
                {
                    QuartoManager.getInstance().deleteQuarto( p );
                    
                    refreshContent();
                }
            }

            catch ( Exception e )
            {
                Utilities.handleException( e );
            }
        }
        
        else
        {
            JOptionPane.showMessageDialog( this, "É necessário selecionar uma pessoa na lista" );
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private framework.parts.QuartoTable quartoTable1;
    // End of variables declaration//GEN-END:variables
}
